// pages/keshi/keshi.js
var util = require('../../utils/util.js');
Page({


  /**
   * 页面的初始数据
   */
  data: {
    imghttp: getApp().globalData.imgHttp,
    list: [],
    listForDate: [],
    selectSmallDepartment: '',
    doctordetailslist: [],
    selectyisheng: '',
    // 点击变色
    calendar: [],
    clickid: 0,
    // 下滑刷新
    List: '',
    pageindex: 1,
    totalPage: 1000,
    scrollViewHeight: "", //医生列表高度
    scrollViewHeightWithCalendar: "", // 有日历时医生列表高度
    // loadStatus: "正在加载", // 数据加载状态
    // 导航条
    navbar: ['按专家预约', '按日期预约'],
    currentTab: 0,
  },
  navbarTap: function (e) {
    var index = e.currentTarget.dataset.idx
    this.setData({
      currentTab: index,
      pageindex: 1,
      total: 1000,
      list: [],
    })
    this.calulateScrollHeight()
    if (index == 0) {
      this.getDoctor()
    } else {
      this.getDoctor(this.data.calendar[this.data.clickid].date)
    }
  },

  getDoctor: function (date) {
    // console.log(this.data.selectSmallDepartment.id)
    var pageindex = this.data.pageindex
    var total = this.data.total
    if (pageindex > total)
      return false
    var opt = {
      current: pageindex,
      size: 10,
      divisionID: this.data.selectSmallDepartment.id,
      // divisionID: 7951,
    }
    if (date != null && date != undefined)
      opt.outpdate = date
    wx.request({
      url: getApp().globalData.myHttp + '/hospital/selectDoctorByParam',
      data: opt,
      method: "GET",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        this.setData({
          list: this.data.list.concat(res.data.obj.records),//将表中查询出来的信息传给list
          pageindex: pageindex + 1,
          total: res.data.obj.pages
        })
      }

    })
  },

  loadPhoto(e) {
    var index = e.currentTarget.dataset.index
    var img = 'list[' + index + '].imgUrl'
    this.setData({
      [img]: '../../image/user.png'
    })
  },

  /**
   * 初始化日历
   */
  initialCalendar() {
    //利用构造函数创建对象
    function calendar(date, week) {
      this.date = date;
      this.week = week;
    }

    //		获取当前时间
    var timeArr = [], timeArrWek = [], weekArr = [];
    var calendarList = [];
    function getTime() {
      var data = [];
      var myDate = new Date();
      //			myDate.getYear();        //获取当前年份(2位)
      myDate.getFullYear();    //获取完整的年份(4位,1970-????)
      myDate.getMonth();       //获取当前月份(0-11,0代表1月)
      myDate.getDate();        //获取当前日(1-31)
      myDate.getDay();  //星期几
      var indexWeek = myDate.getDay();

      for (var i = 1; i <= 7; i++) {
        var date = GetDateStr(i);
        calendarList.push(new calendar(date, getWeek(date)));
      }
    }
    function GetDateStr(AddDayCount) {
      var dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1;//获取当前月份的日期
      var d = dd.getDate();
      return y + "-" + m + "-" + d;
    }
    function getWeek(dateString) {
      var date = new Date(dateString);
      return "星期" + "日一二三四五六".charAt(date.getDay());
    }

    getTime();
    this.setData({
      calendar: calendarList
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(decodeURIComponent(options.selectSmallDepartment))
    this.setData({
      selectSmallDepartment: JSON.parse(decodeURIComponent(options.selectSmallDepartment))
    })
    this.getDoctor()
    this.initialCalendar()
    // this.calulateScrollHeight()
  },

  /**
   * 计算医生列表滚动高度
   */
  calulateScrollHeight() {
    if (this.data.currentTab == 0 && this.data.scrollViewHeight != ""
      || this.data.currentTab == 1 && this.data.scrollViewHeightWithCalendar != "")
      return;
    console.log(this.data.currentTab == 0 ? "计算普通高度" : "计算带日历的高度")
    var that = this
    // 先取出页面高度 windowHeight
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight
        });
      }
    });

    // 然后取出navbar和header的高度
    // 根据文档，先创建一个SelectorQuery对象实例
    let query = wx.createSelectorQuery().in(this)
    // 然后逐个取出navbar和header的节点信息
    // 选择器的语法与jQuery语法相同
    query.select('.navbar').boundingClientRect()
    query.select('.calendar').boundingClientRect()

    // 执行上面所指定的请求，结果会按照顺序存放于一个数组中，在callback的第一个参数中返回
    query.exec((res) => {
      // 分别取出navbar和header的高度
      let navbarHeight = res[0].height
      let headerHeight = res[1].height
      // 然后就是做个减法
      let scrollViewHeight = this.data.windowHeight - navbarHeight - headerHeight
      console.log(scrollViewHeight + "  " + navbarHeight + "  " + headerHeight)
      // 算出来之后存到data对象里面
      if (headerHeight > 0) {
        that.setData({
          scrollViewHeightWithCalendar: scrollViewHeight
        })
      } else {
        that.setData({
          scrollViewHeight: scrollViewHeight
        })
      }
    })
  },

  /**
   * 上拉刷新
   */
  pullupRefresh() {
    this.getDoctor()

  },

  // 跳转到医生详情
  selectyisheng: function (event) {
    this.setData({
      selectyisheng: event.currentTarget.dataset.doctor
    })
    wx.navigateTo({
      url: '../doctordetails/doctordetails?selectyisheng=' + encodeURIComponent(JSON.stringify(this.data.selectyisheng)),
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */

  // 改变选择背景颜色以及点击显示相应日期下的医生列表
  select: function (res) {
    var index = res.currentTarget.dataset.index
    this.setData({
      pageindex: 1,
      total: 1000,
      list: [],
      clickid: index,
    })
    this.getDoctor(this.data.calendar[index].date)

  },
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onShow();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})